/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 * Convert a xml file's datamodel to app's datamodel and vice versa
 *
 * @Original Author: jin Chen
 * @date: Apr 20, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1.xml;

import edu.psu.geovista.app.dataloader.v1.config.auto.*;
import edu.psu.geovista.app.dataloader.v1.config.model.DfPathInfo;
import edu.psu.geovista.app.dataloader.v1.config.model.DfPcpVariableData;
import edu.psu.geovista.app.dataloader.v1.config.model.DfTimeSeriesData;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JaxbPathInfoConvertor {
    ///ObjectFactory  obfty;

    public static edu.psu.geovista.app.dataloader.v1.config.model.Project convert(Project xmlproj){



            //Map map = BeanUtils.describe(xmlproj);
            Pcpdata pcpdata = xmlproj.getPcpdata();
            Tsdata tsdata = xmlproj.getTsdata();
            String id = xmlproj.getID();
            String time = xmlproj.getTime();

        edu.psu.geovista.app.dataloader.v1.config.model.Pcpdata pcppaths = convert(pcpdata);
        edu.psu.geovista.app.dataloader.v1.config.model.TimeSeriesData tspaths = convert(tsdata);

        edu.psu.geovista.app.dataloader.v1.config.model.Project p=new DfPathInfo();
            p.setID(id);
            p.setTime(time);
            p.setTsdata(tspaths);
            p.setPcpdata(pcppaths);
        return p;

    }


    public static edu.psu.geovista.app.dataloader.v1.config.model.Pcpdata convert(Pcpdata xmldata){
        edu.psu.geovista.app.dataloader.v1.config.model.Pcpdata appdata=new DfPcpVariableData();//pdata: pathdata
        List mypaths = appdata.getPaths();
        //copy path info
        java.util.List paths = xmldata.getPaths().getPath();
        for (Iterator iterator = paths.iterator(); iterator.hasNext();) {
            Path path = (Path) iterator.next();

            mypaths.add(path);// put(path.getName() ,path.getValue() );
        }
        //copy attributes info
        java.util.List attrs = xmldata.getSelectedAttributes().getAttr(); //return AttributeImpl
        List <String> stAttrs=new ArrayList<String>(attrs.size() );
        for (Iterator iterator = attrs.iterator(); iterator.hasNext();) {
            Attribute attr = (Attribute) iterator.next();
            String attname = attr.getName();
            stAttrs.add(attname);
        }
        appdata.setSelectedVariableNames(stAttrs);

        boolean load = xmldata.isLoad(); //if the data need be loade
        appdata.setLoad(load);


        //shape
        boolean projected = xmldata.getShapedata().isProjected();
       appdata.setShapeProjected(projected);



        return appdata;


    }
    public static edu.psu.geovista.app.dataloader.v1.config.model.TimeSeriesData convert(Tsdata xmldata){
        edu.psu.geovista.app.dataloader.v1.config.model.TimeSeriesData appdata=new DfTimeSeriesData();//pdata: pathdata
        List  mypaths = appdata.getPaths();
        //copy path info
        java.util.List paths = xmldata.getPaths().getPath();
        for (Iterator iterator = paths.iterator(); iterator.hasNext();) {
            Path path = (Path) iterator.next();
            mypaths.add(path); //put(path.getName() ,path.getValue() );
        }
        //copy attributes info
        java.util.List attrs = xmldata.getSelectedAttributes().getAttr(); //return AttributeImpl
        List <String> stAttrs=new ArrayList<String>(attrs.size() );
        for (Iterator iterator = attrs.iterator(); iterator.hasNext();) {
            Attribute attr = (Attribute) iterator.next();
            String attname = attr.getName();
            stAttrs.add(attname);
        }
        appdata.setSelectedVariableNames(stAttrs);

        boolean load = xmldata.isLoad(); //if the data need be loade
        appdata.setLoad(load);

        return appdata;


    }
   /* private ObjectFactory getObjectFactory(){
        if(obfty==null){
            obfty=new ObjectFactory();
        }
        return obfty;
    }*/
}
